package com.xnew.utils;

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import org.apache.commons.lang3.StringUtils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * AES 加密
 */
@SuppressWarnings("restriction")
public class AESEncryption {

    /**
     * 默认加密的KEY
     */
    private static final String KEY_DEFAULT = "8G5M4Ff9hel8fUA9";

    public static String DES = "AES"; // optional value AES/DES/DESede

    public static String CIPHER_ALGORITHM = "AES"; // optional value AES/DES/DESede


    public  Key getKey(String strKey) {
        try {
            KeyGenerator _generator = KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(strKey.getBytes());
            _generator.init(128, secureRandom);
            return _generator.generateKey();
        } catch (Exception e) {
            throw new RuntimeException(" 初始化密钥出现异常 ");
        }
    }

    /**
     * 加密方法
     */
    public  String encrypt(String data, String key){

        SecureRandom sr = new SecureRandom();
        if (key == null || "".equals(key)) {
            key = KEY_DEFAULT;
        }
        try {
        Key secureKey = getKey(key);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secureKey, sr);
        byte[] bt = cipher.doFinal(data.getBytes());
        String strS = new BASE64Encoder().encode(bt);
        return strS;
        } catch (Exception e) {
            throw new RuntimeException("加密出现异常 ");
        }
    }


    /**
     * 解密方法
     */
    public  String decrypt(String message, String key) {
        SecureRandom sr = new SecureRandom();
        if (key == null || "".equals(key)) {
            key = KEY_DEFAULT;
        }
        try {
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            Key secureKey = getKey(key);
            cipher.init(Cipher.DECRYPT_MODE, secureKey, sr);
            byte[] res = new BASE64Decoder().decodeBuffer(message);
            if(res!=null && res.length > 0 && res.length % 16 ==0){
                res = cipher.doFinal(res);
                return new String(res);
             }
        } catch (Exception e) {
            throw new RuntimeException(" 解密出现异常 ");
        }
        return "";
    }

    /**
     * 配置文件解密方法
     */
    public static String decNewValue(String value){
        //解密
        if(StringUtils.isNotBlank(value)){
            AESEncryption aes = new AESEncryption();
            String decResult = aes.decrypt(value,"");
            if(StringUtils.isNotBlank(decResult)){
                return decResult;
            }
        }
        return value;
    }
    
    //对smp.properties配置文件中密码加密
    /*public static void main(String[] args) {
    	 AESEncryption aes = new AESEncryption();
         String decResult = aes.encrypt("SMP@2018","");
         System.out.println(decResult);
	}*/
}
